package com.appspot.etao.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.appspot.etao.model.ItemCatModel;
import com.appspot.etao.model.ShopCatModel;
import com.appspot.etao.model.ShopModel;
import com.appspot.etao.model.TaobaokeItemModel;
import com.appspot.etao.model.TaobaokeShopModel;
import com.appspot.etao.model.UserModel;
import com.taobao.top.domain.ItemCat;
import com.taobao.top.domain.Shop;
import com.taobao.top.domain.ShopCat;
import com.taobao.top.domain.TaobaokeItem;
import com.taobao.top.domain.TaobaokeShop;
import com.taobao.top.domain.User;

/**
 * 将远程Top对象（R），转换为本地对象（L）
 * 
 * @author Administrator
 * 
 * @param <R>
 * @param <L>
 */
public class ConvertUtil {

//	class C<T> {
//		T makeT() {
//			return new T();
//		}
//	}
//
//	class D<S> {
//		C<S> makeC() {
//			return new C<S>();
//		}
//	}

//	public static <L, R> List<L> convert(List<R> rList) {
//		List<L> lList = new ArrayList<L>();
//
//		for (R r : rList) {
////			lList.add(new L(r));
//		}
//
//		return lList;
//	}

	/**
	 * 将Top TaobaokeItem对象转换为本地对象
	 * 为了方便页面取数，增加cid类别
	 */
	public static List<TaobaokeItemModel> convertTaobaokeItemList(List<TaobaokeItem> taobaokeItemList, String cid) {
		List<TaobaokeItemModel> taobaokeItemModelList = new ArrayList<TaobaokeItemModel>();

		if (taobaokeItemList == null) return taobaokeItemModelList;
		
		for (TaobaokeItem taobaokeItem : taobaokeItemList) {
			TaobaokeItemModel taobaokeItemModel = new TaobaokeItemModel(taobaokeItem, cid);
			taobaokeItemModelList.add(taobaokeItemModel);
		}

		return taobaokeItemModelList;
	}

	/**
	 * 将Top Shop对象转换为本地对象
	 * @param shopList
	 * @return
	 */
	public static List<ShopModel> convertShopList(List<Shop> shopList) {
		List<ShopModel> shopModelList = new ArrayList<ShopModel>();

		if (shopList == null) return shopModelList;

		for (Shop shop : shopList) {
			shopModelList.add(new ShopModel(shop));
		}

		return shopModelList;
	}
	
	/**
	 * 将TaobaokeShop对象转换为本地对象
	 * @param shopList
	 * @return
	 */
	public static List<TaobaokeShopModel> convertTaobaokeShopList(List<TaobaokeShop> shopList, String cid) {
		List<TaobaokeShopModel> shopModelList = new ArrayList<TaobaokeShopModel>();

		if (shopList == null) return shopModelList;

		for (TaobaokeShop shop : shopList) {
			shopModelList.add(new TaobaokeShopModel(shop, cid));
		}

		return shopModelList;
	}
	
	/**
	 * 将Top User对象转换为本地对象
	 * @param userList
	 * @return
	 */
	public static List<UserModel> convertUserList(List<User> userList) {
		List<UserModel> userModelList = new ArrayList<UserModel>();

		if (userList == null) return userModelList;

		for (User user : userList) {
			userModelList.add(new UserModel(user));
		}

		return userModelList;
	}
	
	/**
	 * 将Top ShopCat对象转换为本地对象
	 * @param shopCatList
	 * @return
	 */
	public static List<ShopCatModel> convertShopCatList(List<ShopCat> shopCatList) {
		List<ShopCatModel> shopCatModelList = new ArrayList<ShopCatModel>();

		if (shopCatList == null) return shopCatModelList;

		for (ShopCat shopCat : shopCatList) {
			shopCatModelList.add(new ShopCatModel(shopCat));
		}

		return shopCatModelList;
	}
	
	/**
	 * 将ItemCat对象转换为本地对象
	 * @param itemCatList
	 * @return
	 */
	public static List<ItemCatModel> convertItemCatList(List<ItemCat> itemCatList) {
		List<ItemCatModel> itemCatModelList = new ArrayList<ItemCatModel>();

		if (itemCatList == null) return itemCatModelList;

		for (ItemCat shopCat : itemCatList) {
			itemCatModelList.add(new ItemCatModel(shopCat));
		}

		return itemCatModelList;
	}
	
	/**
	 * 
	 * @param str
	 * @param delimiter
	 * @return
	 */
	public static List<String> convertStringToList(String str, String delimiter) {
		List<String> strList = new ArrayList<String>();
		
		StringTokenizer token = new StringTokenizer(str, delimiter);
		while (token.hasMoreElements()) {
			strList.add(token.nextToken());
		}

		return strList;
	}
}
